package org.elanfox.intellicommunity.msg.server.service;

import org.apache.http.MethodNotSupportedException;
import org.elanfox.intellicommunity.common.ienum.base.CodeDescEnumHelper;
import org.elanfox.intellicommunity.msg.ienum.CmdEnums;
import org.elanfox.intellicommunity.msg.server.service.base.CmdReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/3/17
 */
@Service
public class CmdReaderBuilder {
    @Autowired
    private CmdNormalReader cmdNormalReader;

    public CmdEnums.Code cmdCode(String hexStr) {
        if (!hexStr.toUpperCase().startsWith("F")) {
            return null;
        }
        String hexCmdCode = hexStr.substring(26, 28);
        CmdEnums.Code cmdCode = CodeDescEnumHelper.getEnumTypeByCode(CmdEnums.Code.class, hexCmdCode);
        if (cmdCode == null) {
            return null;
        }

        return cmdCode;
    }

    public CmdReader build(CmdEnums.Code cmdCode) throws MethodNotSupportedException {
        switch (cmdCode.getType()) {
            case NORMAL:
                return cmdNormalReader;
            default:
                throw new MethodNotSupportedException("method not supported for cmd type " + cmdCode.getType().toString());
        }
    }
}
